/*
 * ComerciosBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 11:45 PM
 */
package org.comercios.registros;
  
import org.mybeans.Sesion;
import org.mybeans.ComerciosBeans;
 
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
 
/**
 *
 * @author  avbravo
 */
public class ComerciosBajadosBusquedaFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    ComerciosBeans comerciosBeans;

    /** Creates new form ComerciosBusquedaFrame */
    public ComerciosBajadosBusquedaFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */
        String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes  " +
                " where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='no' " +
                " and comercios.cedula = contribuyentes.cedula " +
                " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' " +
                " and contribuyentes.activo='si' " +
                " order by comercio";
        CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldComercio = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldIdComercio = new javax.swing.JTextField();
        jButtonRegresar = new javax.swing.JButton();
        jButtonVerTodos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("BUSQUEDA DE COMERCIOS BAJADOS");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setOpaque(false);

        jLabel2.setText("Comercio");

        jTextFieldComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldComercioActionPerformed(evt);
            }
        });

        jLabel3.setText("Codigo");

        jTextFieldIdComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdComercioActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonVerTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/view.png"))); // NOI18N
        jButtonVerTodos.setToolTipText("Ver Todos");
        jButtonVerTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(12, 12, 12)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonVerTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(69, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonVerTodos)
                    .addComponent(jButtonRegresar))
                .addContainerGap())
        );

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "codigo", "comercio", "contribuyente", "cedula"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jXTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonVerTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerTodosActionPerformed

    String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='no' and comercios.cedula = contribuyentes.cedula " +
            " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' " +
            " and contribuyentes.activo='si' " +
            " order by comercio";
    CargarTodosRegistros(sql);
}//GEN-LAST:event_jButtonVerTodosActionPerformed

private void jTextFieldComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldComercioActionPerformed

    BuscarComercios();
}//GEN-LAST:event_jTextFieldComercioActionPerformed

private void jTextFieldIdComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdComercioActionPerformed

    BuscarCodigoComercio();
}//GEN-LAST:event_jTextFieldIdComercioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosBajadosBusquedaFrame().setVisible(true);
            }
        });
    }

    private void BuscarComercios() {
        try {


            String nombre = (String) this.jTextFieldComercio.getText();
            String sql = "select comercios.idcomercio, comercios.comercio,contribuyentes.contribuyente, comercios.cedula from comercios, contribuyentes where comercios.comercio like '%" + nombre + "%' and comercios.idmunicipio = '" + sesion.getIdmunicipio() + "' and comercios.activo ='no' and comercios.cedula = contribuyentes.cedula " +
                    " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' " +
                    " and contribuyentes.activo='si' " +
                    " order by comercio";
            CargarTodosRegistros(sql);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void BuscarCodigoComercio() {
        try {
            String idComercio = (String) this.jTextFieldIdComercio.getText();
            String sql = "select comercios.idcomercio, comercios.comercio,contribuyentes.contribuyente, comercios.cedula from comercios, contribuyentes where comercios.idcomercio like '%" + idComercio + "%' and comercios.idmunicipio = '" + sesion.getIdmunicipio() + "' and comercios.activo ='no' and comercios.cedula = contribuyentes.cedula " +
                    " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' " +
                    " and contribuyentes.activo='si' " +
                    " order by comercio";
            CargarTodosRegistros(sql);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. BuscarCodigoComercio()", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void CargarTodosRegistros(String sql) {
        try {

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();

            modelo = new DefaultTableModel();
            // Creamos las columnas.
            modelo.addColumn("codigo");
            modelo.addColumn("comercio");
            modelo.addColumn("contribuyente");
            modelo.addColumn("cedula");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[4]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idcomercio");
                fila[1] = rs.getString("comercio");
                fila[2] = rs.getString("contribuyente");
                fila[3] = rs.getString("cedula");
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);
            //aris++
            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        jTextFieldIdComercio.setText(String.valueOf(jXTable1.getValueAt(fila, 0)));
                        jTextFieldComercio.setText(String.valueOf(jXTable1.getValueAt(fila, 1)));
                        String contribuyente = String.valueOf(jXTable1.getValueAt(fila, 2));
                        String cedula = String.valueOf(jXTable1.getValueAt(fila, 3));

                        String idcomercio = (String) jTextFieldIdComercio.getText();
                        String comercio = (String) jTextFieldComercio.getText();
                        CargarComercios();

                    }
                }
            });

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. CargarTodosRegistros()", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void CargarComercios() {
        try {
            String IdComercio = this.jTextFieldIdComercio.getText();
            if (IdComercio == null || IdComercio.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio = ? and comercios.idcomercio = ? and comercios.cedula = contribuyentes.cedula " +
                 " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' "+
                " and contribuyentes.activo='si' "+
                " order by comercio";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, IdComercio);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No existen registros de comercios con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String comercio = rs.getString("comercio");
            String cedula = rs.getString("cedula");
            String contribuyente = rs.getString("contribuyente");
            String Ruc = rs.getString("ruc");
            String Descripcion = rs.getString("descripcion");
            String Direccion = rs.getString("direccion");
            String IdCorregimiento = rs.getString("idcorregimiento");
            String Telefono = rs.getString("telefono");
            String Hipotecado = rs.getString("hipotecado");
            String Bajado = rs.getString("bajado");
            String Traspaso = rs.getString("traspaso");
            String Estatal = rs.getString("estatal");
            String Secuestrado = rs.getString("secuestrado");
            String Activo = rs.getString("activo");


            if (comercio == null || comercio.equals("")) {
                JOptionPane.showMessageDialog(this, "El comercio esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            comerciosBeans.setIdComercio(IdComercio);
            comerciosBeans.setComercio(comercio);
            comerciosBeans.setCedula(cedula);
            comerciosBeans.setContribuyente(contribuyente);
            comerciosBeans.setRuc(Ruc);
            comerciosBeans.setDescripcion(Descripcion);
            comerciosBeans.setDireccion(Direccion);
            comerciosBeans.setIdCorregimiento(IdCorregimiento);
            comerciosBeans.setTelefono(Telefono);
            comerciosBeans.setHipotecado(Hipotecado);
            comerciosBeans.setBajado(Bajado);
            comerciosBeans.setTraspaso(Traspaso);
            comerciosBeans.setEstatal(Estatal);
            comerciosBeans.setSecuestrado(Secuestrado);
            comerciosBeans.setActivo(Activo);
            sesion.setIdComercio(IdComercio);
            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVerTodos;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldComercio;
    private javax.swing.JTextField jTextFieldIdComercio;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
